<template>
  <div class="dialog">
    <el-dialog :title="'作业记录' + titleText" :visible.sync="showDialog" width="45%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="鱼塘名称:" prop="fishPondId" class="w50p">
          <el-select filterable clearable v-model="formData.fishPondId" @change="queryBatch">
            <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName" :value="item.fishPondId" />
          </el-select>
        </el-form-item>
        <el-form-item label="批次编号:" prop="productionBatchId" class="w50p">
          <el-select filterable clearable v-model="formData.productionBatchId">
            <el-option v-for="(item, index) in batchList" :key="index" :label="`${item.mnecode}_${item.varietyName}`"
              :value="item.productionBatchId" />
          </el-select>
        </el-form-item>
        <el-form-item label="排水量：" class="w50p" prop="discharge">
          <el-input v-model="formData.discharge" clearable placeholder="请输入排水量">
            <template slot="append">立方米</template>
          </el-input>
        </el-form-item>
        <el-form-item label="入水量：" class="w50p" prop="inflow">
          <el-input v-model="formData.inflow" clearable placeholder="请输入入水量">
            <template slot="append">立方米</template>
          </el-input>
        </el-form-item>
        <el-form-item label="氨氮含量：" class="w50p" prop="nhnContent">
          <el-input v-model="formData.nhnContent" clearable placeholder="请输入氨氮含量">
            <template slot="append">毫克/升</template>
          </el-input>
        </el-form-item>
        <el-form-item label="亚盐含量：" class="w50p" prop="sulfiteContent">
          <el-input v-model="formData.sulfiteContent" clearable placeholder="请输入亚盐含量">
            <template slot="append">毫克/升</template>
          </el-input>
        </el-form-item>
        <el-form-item label="作业人:" prop="staffName" class="w50p">
          <el-select filterable clearable v-model="formData.staffName">
            <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName" :value="item.staffName" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业时间：" class="w50p" prop="workDateTime">
          <el-date-picker v-model="formData.workDateTime" type="datetime" placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" clearable />
        </el-form-item>
        <!-- <el-form-item label="逾期时间：" class="w50p" prop="overdueDateTime">
          <el-date-picker
              v-model="formData.overdueDateTime"
              type="datetime"
              placeholder="选择日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
            />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import WaterChangeOperationApi from "@/api/operation-management/water-change-operation"
import BatchMangeApi from "@/api/batch-mange"
import { dialogMixin } from "@/utils/dialogMixin";
export default {
  name: '',
  components: {},
  mixins: [dialogMixin],
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
    staffList: {
      type: [Array],
      default: () => {
        return []
      }
    },
    pondList: {
      type: [Array],
      default: () => {
        return []
      }
    },
    materialList: {
      type: [Array],
      default: () => {
        return []
      }
    },
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      rules: {
        storeHouseName: [
          { required: true, message: "请输入批次编号", trigger: ["change", "blur"] }
        ],
        area: [
          { required: true, message: "请输入批次面积", trigger: ["change", "blur"] }, {
            validator: checkInt, trigger: ["change", "blur"]
          }
        ],
      },
      baseurl: this.$baseurl.fileServiceUrl + 'base/files/upload',
      headers: {
        token: sessionStorage.getItem('token'),
        homeBaseId: sessionStorage.getItem('homeBaseId'),
        homeBaseType: sessionStorage.getItem('homeBaseType'),
      },
      images: [],
      fileIdList: [],
      formData: {},
      batchList: [],//批次
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
          workDateTime: this.getNowTime()
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject

      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    queryBatch(e) {
      BatchMangeApi.queryBatchByPondId({ pondId: e }).then(res => {
        this.batchList = res.data
      })
    },
    save(formData) {
      this.formData.overdueFlag = false
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            WaterChangeOperationApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            WaterChangeOperationApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
